package ad;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import bo.DBConnection;
import bo.book;
import vo.User;

public class AdDao implements IAdDao {

	protected static String add_sql="insert into namecard(id,name,sex,username,userpwd,mail) values(?,?,?,?,?,?)";
	protected static String search_sql="select * from namecard where id=? and username=? and userpwd=?";
	protected static String delete_sql="delete from namecard where id=? and username=? and userpwd=?";
	protected static String update_sql="update namecard set id=?,name=?,sex=?,username=?,userpwd=?,mail=? where id=? and username=? and userpwd=?";
	protected static String show_sql="select * from namecard";
	protected static String deleteuser_sql="delete from user where username=?";
	protected static String showuser_sql="select * from user";

	@Override
	public book add(book b) throws Exception {
		// TODO Auto-generated method stub
		Connection conn=DBConnection.getDBConnection();
		PreparedStatement pstmt=conn.prepareStatement(add_sql);
		pstmt.setString(1,b.getId());
		pstmt.setString(2,b.getName());
		pstmt.setString(3,b.getSex());
		pstmt.setString(4,b.getUsername());
		pstmt.setString(5,b.getUserpwd());
		pstmt.setString(6,b.getMail());
		pstmt.executeUpdate();
		DBConnection.free(conn, pstmt, null);
		return b;
		
	}

	@Override
	public void delete(book b) throws Exception {
		// TODO Auto-generated method stub
		Connection conn=DBConnection.getDBConnection();
		PreparedStatement pstmt=conn.prepareStatement(delete_sql);
		pstmt.setString(1,b.getId());
		pstmt.setString(2, b.getUsername());
		pstmt.setString(3, b.getUserpwd());
		pstmt.executeUpdate();
		DBConnection.free(conn, pstmt, null);

	}

	@Override
	public book search(book b) throws Exception {
		// TODO Auto-generated method stub
		Connection conn=DBConnection.getDBConnection();
		PreparedStatement pstmt=conn.prepareStatement(search_sql);
		book b1=null;
		pstmt.setString(1,b.getId());
		pstmt.setString(2, b.getUsername());
		pstmt.setString(3, b.getUserpwd());
		ResultSet rs=pstmt.executeQuery();
		if(rs.next()) {
			b1=new book();
			b1.setId(rs.getString(1));
			b1.setName(rs.getString(2));
			b1.setSex(rs.getString(3));
			b1.setUsername(rs.getString(4));
			b1.setUserpwd(rs.getString(5));
			b1.setMail(rs.getString(6));
			
		}
		DBConnection.free(conn, pstmt, rs);
		return b1;
		
	}

	@Override
	public void update(book b) throws Exception {
		// TODO Auto-generated method stub
		Connection conn=DBConnection.getDBConnection();
		PreparedStatement pstmt=conn.prepareStatement(update_sql);
		pstmt.setString(1, b.getId());
		pstmt.setString(2,b.getName());
		pstmt.setString(3, b.getSex());
		pstmt.setString(4, b.getUsername());
		pstmt.setString(5,b.getUserpwd());
		pstmt.setString(6,b.getMail());
		pstmt.setString(7, b.getId());
		pstmt.setString(8, b.getUsername());
		pstmt.setString(9, b.getUserpwd());
		int rowCount=pstmt.executeUpdate();
		if(rowCount==0) {
			throw new Exception("Update Error,card id is"+b.getId());
		}
		DBConnection.free(conn, pstmt, null);

	}

	@Override
	public List<book> show() throws Exception {
		// TODO Auto-generated method stub
		Connection conn=DBConnection.getDBConnection();
		PreparedStatement pstmt=conn.prepareStatement(show_sql);
		ResultSet rs=pstmt.executeQuery();
		List<book> b=new ArrayList<book>();
		while(rs.next()) {
			book b1=new book();
			b1.setId(rs.getString(1));
			b1.setName(rs.getString(2));
			b1.setSex(rs.getString(3));	
			b1.setUsername(rs.getString(4));
			b1.setUserpwd(rs.getString(5));
			b1.setMail(rs.getString(6));
			b.add(b1);
		}
		DBConnection.free(conn, pstmt, rs);
		return b;
		
	}
	@Override
	public void deleteuser(User u) throws Exception {
		// TODO Auto-generated method stub
		Connection conn=DBConnection.getDBConnection();
		PreparedStatement pstmt=conn.prepareStatement(deleteuser_sql);
		pstmt.setString(1,u.getXm());
		pstmt.executeUpdate();
		DBConnection.free(conn, pstmt, null);

	}
	
	public List<User> show_user() throws Exception {
		// TODO Auto-generated method stub
		Connection conn=DBConnection.getDBConnection();
		PreparedStatement pstmt=conn.prepareStatement(showuser_sql);
		ResultSet rs=pstmt.executeQuery();
		List<User> list=new ArrayList<User>();
		while(rs.next()) {
			User n1=new User();
			n1.setXm(rs.getString(1));
			n1.setMm(rs.getString(2));
			n1.setRealxm(rs.getString(3));
			list.add(n1);
		}
		DBConnection.free(conn, pstmt, rs);
		return list;
	}

}
